﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using EuroManager.Domain.Simulator.Engine;
using EuroManager.Domain.Simulator.Helpers;

namespace EuroManager.Domain.Simulator.World
{
    public class Pitch : IPitch
    {
        private static readonly float width = 106.5f;
        private static readonly float height = 71.5f;
        private static readonly Vector2D center = new Vector2D(width / 2, height / 2);

        public float Width
        {
            get { return width; }
        }

        public float Height
        {
            get { return height; }
        }

        public Vector2D Center
        {
            get { return center; }
        }

        public Vector2D GoalLocation(PitchSide side)
        {
            return MapToMetricalLocation(side, 0, 0.5f);
        }

        public Vector2D MapToMetricalLocation(PitchSide side, Vector2D location)
        {
            return MapToMetricalLocation(side, location.X, location.Y);
        }

        public Vector2D MapToMetricalLocation(PitchSide side, float x, float y)
        {
            if (side == PitchSide.Left)
            {
                return MapToMetricalLocation(x, y);
            }
            else
            {
                return MapToMetricalLocation(1 - x, 1 - y);
            }
        }

        public Vector2D MapToProportionalLocation(PitchSide side, Vector2D location)
        {
            return MapToProportionalLocation(side, location.X, location.Y);
        }

        public Vector2D MapToProportionalLocation(PitchSide side, float x, float y)
        {
            x = Maths.MinMax(0, x, width);
            y = Maths.MinMax(0, y, height);

            if (side == PitchSide.Left)
            {
                return MapToProportionalLocation(x, y);
            }
            else
            {
                return MapToProportionalLocation(width - x, height - y);
            }
        }

        private Vector2D MapToMetricalLocation(float x, float y)
        {
            return new Vector2D(x * width, y * height);
        }

        private Vector2D MapToProportionalLocation(float x, float y)
        {
            return new Vector2D(x / width, y / height);
        }
    }
}
